{% extends 'layout/basicplus.html' %}

{% block title %}register{% endblock %}

{% block css %}
    <style>
        .account {
            width: 500px;
            border: 1px solid #dddddd;
            border-radius: 10px;
            box-shadow: 10px 10px 10px #aaa;
            padding: 20px 40px;

            margin-left: auto;
            margin-right: auto;
            margin-top: 100px;

        }

        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>

{% endblock %}

{% block content %}
    <body class="gray-bg">
    <div class="loginscreen animated fadeInDown account">
        <div>
            <h1 style="text-align: center;"><strong>注 册</strong></h1>
            <p style="text-align: center;">Create account</p>



            <form id="regform" method="POST" novalidate>
                {% csrf_token %}
                {% for field in form %}
                    {% if field.name == 'code' %}
                        <div class="form-group">
                            <label for="{{ field.id_for_label }}"><strong
                                    style="font-size: 20px;">{{ field.label }}</strong></label>
                            <div class="row">
                                <div class="col-md-5">
                                    {{ field }}
                                    <span class="error-msg">{{ field.errors.0 }}</span>
                                </div>
                                <div class="col-md-5">
                                    <input id="btnSms" type="button" class="btn btn-default" value="获取手机验证码">
                                </div>
                            </div>

                        </div>
                    {% else %}
                        <div class="form-group">
                            <label for="{{ field.id_for_label }}"><strong
                                    style="font-size: 20px;">{{ field.label }}</strong></label>
                            {{ field }}
                            <span class="error-msg">{{ field.errors.0 }}</span>
                        </div>
                    {% endif %}
                {% endfor %}
                <div class="row">
                    <div class="col-md-4"></div>
                    <div class="col-md-3">
                        <input id="btnSub" type="button" class="btn btn-primary" style="width: 100px" value="注 册">
                    </div>
                    <div class="col-md-1">

                    </div>
                    <div class="col-md-4" style="margin-top: 10px;"><span class="glyphicon glyphicon-share-alt"
                                                                          aria-hidden="true"></span><a
                            href="/web/loginplus">返回上一级</a>
                    </div>
                </div>

            </form>
            <p class="m-t" style="text-align: right"><small>2023 毕设</small></p>
        </div>
    </div>


    </body>

{% endblock %}

{% block js %}
    <script>


        $(function () {
            bindClickBtnSms();
            bindClickBtnSub();
        });

        function bindClickBtnSub() {
            $('#btnSub').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: "/web/registerplus/",
                    type: "POST",
                    data: $('#regform').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data;
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty();
                var mobilePhone = $('#id_mobile_phone').val();

                $.ajax({
                    url: "/web/send/sms/",
                    type: "GET",
                    data: {mobile_phone: mobilePhone, tpl: "register"},
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            $('#btnSms').val('发送成功').prop('disabled', true);
                            var time = 59
                            var event = setInterval(function () {
                                $('#btnSms').val(time + '秒后重新发送');
                                time = time - 1;
                                if (time == 0) {
                                    clearInterval(event);
                                    $('#btnSms').val('获取手机验证码').prop('disabled', false);
                                }
                            }, 1000)

                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }


    </script>

{% endblock %}